//
//  InfoVC.m
//  coffee
//
//  Created by student4 on 2020/12/6.
//  Copyright © 2020 MOSAD. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "InfoVC.h"
#import "../../util/UserRequest.h"
@interface InfoVC()<UITableViewDelegate,UITableViewDataSource,RenameDelegate>{}
@property (nonatomic,assign) UserInfo *info;
@end

@implementation InfoVC

-(void)viewDidLoad{
    [super viewDidLoad];
    _info_list = [@[
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"用户ID",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"用户邮箱",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"用户名",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"用户级别",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"个人经历",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"性别",@"value":@""}],
                    [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"昵称",@"value":@""}]] mutableCopy];
    _infoview = [[InfoView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [_infoview setDelegate:self];
    [_infoview setDataSource:self];
    [_infoview viewInit];
    [self.view addSubview:_infoview];
}

-(void)setInfo:(UserInfo*)info{
    _info = info;
    // 简单设置一下内容
    [_info_list[0] setValue:[info ID] forKey:@"value"];
    [_info_list[1] setValue:[info Email] forKey:@"value"];
    [_info_list[2] setValue:[[info Info]Name] forKey:@"value"];
    [_info_list[3] setValue:[info Class]==0?@"超级用户":@"普通用户" forKey:@"value"];
    [_info_list[4] setValue:[[info Info]Bio] forKey:@"value"];
    [_info_list[5] setValue:[info Info].Gender==1?@"男":@"女" forKey:@"value"];
    [_info_list[6] setValue:[info Info].NikeName forKey:@"value"];
    
    [_infoview reloadData];
    [self.view reloadInputViews];
}
-(void)rename{
    RenameVC *vc = [[RenameVC alloc]init];
    [[vc navigationItem]setTitle:@"修改用户名"];
    [vc setDelegate:self];
    [vc viewDidLoad];
    [self.navigationController pushViewController:vc animated:NO];
}
-(void)resetName:(NSString *)newName{
    [[_info Info]setName:newName];
    [_info_list[2] setValue:newName forKey:@"value"];
    [_infoview reloadData];
    [self.view reloadInputViews];
}
#pragma mark UITableViewDataSource
-(NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _info_list.count;
}
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    NSDictionary *property = _info_list[indexPath.row];
    cell.textLabel.text = property[@"title"];
    if ([property[@"title"]isEqualToString:@"用户名"]) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@  >",property[@"value"]];
    } else {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@   ",property[@"value"]];
    }
    return cell;
}
-(CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 60.0f;
}

-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *property = _info_list[indexPath.row];
    NSString *option = property[@"title"];
    if ([option isEqualToString:@"用户名"]) {
        [self rename];
    }
}
@end

@implementation RenameVC

-(void)viewDidLoad{
    [super viewDidLoad];
    [self setRenameview:[[RenameView alloc] initWithFrame:[UIScreen mainScreen].bounds]];
    [_renameview viewInit];
    [[_renameview rename]addTarget:self action:@selector(rename) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_renameview];
}
-(void)rename{
    NSString *newName = [[[_renameview iname]text]copy];
    UserReq* manager = [UserReq defaultUserRequest];
    [manager rename:newName success:^{
        [self->_delegate resetName:newName];
        [[self navigationController]popViewControllerAnimated:NO];
    } failure:^(NSString *reason) {
        NSLog(@"%@",reason);
    }];
}
@end
